﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace game
{
    public class BlasterCell : Projectile
    {
        protected static VertexPositionTexture[] verts = new VertexPositionTexture[4]{
            new VertexPositionTexture((new Vector3(-150,0,-3000)), new Vector2(0,0)),
            new VertexPositionTexture((new Vector3(150,0,-3000)), new Vector2(0,1)),
            new VertexPositionTexture((new Vector3(150,0,3000)), new Vector2(1,1)),
            new VertexPositionTexture((new Vector3(-150,0,3000)), new Vector2(1,0)),
        };

        protected static short[] indices = new short[6] { 0, 1, 2, 0, 2, 3 };

        private static Texture textureGreen;
        private static Texture textureRed;
        private static Texture textureBlue;
        private static Effect effect;
        private static EffectTechnique effectTech;
        private static EffectParameter ep_gWVP;
        private static EffectParameter ep_gInvWorld;
        private static EffectParameter ep_gTex;
        private static EffectParameter ep_gEyePosW;

        private static VertexDeclaration vertexDecl;

        static BlasterCell()
        {
            textureGreen = ShooterGame.Instance.Content.Load<Texture>("Textures\\blaster_cell_green");
            textureRed = ShooterGame.Instance.Content.Load<Texture>("Textures\\blaster_cell_red");
            textureBlue = ShooterGame.Instance.Content.Load<Texture>("Textures\\blaster_cell_blue");

            effect = ShooterGame.Instance.Content.Load<Effect>("Effects\\BlasterCell");
            effectTech = effect.Techniques["BlasterCell"];
            ep_gWVP = effect.Parameters["gWVP"];
            ep_gInvWorld = effect.Parameters["gInvWorld"];
            ep_gTex = effect.Parameters["gTex"];
            ep_gEyePosW = effect.Parameters["gEyePosW"];

            vertexDecl = new VertexDeclaration(ShooterGame.Instance.GraphicsDevice,
                    VertexPositionTexture.VertexElements);
        }

        public enum Color
        {
            GREEN,
            RED,
            BLUE,
        }

        private Color color;

        public BlasterCell(Scene scene, Ship shooter, int layerIndex, Vector3 position, Vector3 rotation, Color color)
            : base(scene, shooter, null, layerIndex, position, rotation)
        {
            this.color = color;

            this.moveSpeed = 220.0f;
            this.Health = 1.0f;
            this.MaxHealth = this.Health;
            this.damage = 5.0f;
            this.lifeTimeMs = 8000;

            bSphere = new BoundingSphere(new Vector3(0, 0, 0), 100);
        }

        protected override void InitParticleSystem(Scene scene)
        {
            // empty, in order to override default behaviour
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
        }

        public override void DrawTransparent(GameTime gameTime)
        {
            ShooterGame.Instance.GraphicsDevice.VertexDeclaration
                = vertexDecl;

            ep_gWVP.SetValue(World * scene.ActiveCamera.View * scene.ActiveCamera.Projection);
            ep_gInvWorld.SetValue(Matrix.Invert(World));
            if(color == Color.GREEN)
                ep_gTex.SetValue(textureGreen);
            else if(color == Color.RED)
                ep_gTex.SetValue(textureRed);
            else if(color == Color.BLUE)
                ep_gTex.SetValue(textureBlue);
            ep_gEyePosW.SetValue(scene.ActiveCamera.Position);
            effect.CurrentTechnique = effectTech;

            effect.Begin();
            foreach (EffectPass pass in effect.CurrentTechnique.Passes)
            {
                pass.Begin();

                ShooterGame.Instance.GraphicsDevice.DrawUserIndexedPrimitives
                    <VertexPositionTexture>(
                    PrimitiveType.TriangleList, verts, 0, 4, indices, 0, 2);                    
                pass.End();
            }
            effect.End();
        }
    }
}
